package com.easycar.common.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.easycar.common.exception.InitializationException;

import java.util.HashMap;
import java.util.List;


/**
 * 获取Android   Androidmanifest 信息
 */
public class AndroidMetaData {

    /**
     * 获取 Android manifest 信息
     * @param context  上下文
     * @param metaKey  键
     * @return  返回 键的值
     */

    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String metaValue = null;
        if (null == context || null == metaKey) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                metaValue = metaData.getString(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return metaValue;
    }

    /**
     * Get Application Meta Data by Application Context
     * <P>通过应用的上下文获取对于列表键的信息
     *
     * @param context context
     * @param keys    String keys
     * @return HashMap<\String, String> metaDataContent or Null
     */
    public static HashMap<String, String> getApplicationMetaData(Context context, List<String> keys) {
        HashMap<String, String> mdc = null;
        ApplicationInfo info;
        Bundle metaData;
        try {
            info = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != info) {
                metaData = info.metaData;
                if (null != metaData) {
                    mdc = new HashMap<>();
                    for (String string : keys) {
                        mdc.put(string, metaData.getString(string));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mdc;
    }


    private AndroidMetaData() {
      throw   new InitializationException(getClass().getName());
    }


}
